'************************************************************************************
' Copyright © 2003-2005 Kathleen Dollard
'
' This software is provided 'as-is', without any express or implied warranty. In no 
' event will the author be held liable for any damages arising from the use of this
' software.
' 
' You may use this software for any purpose, including commercial applications, 
' and you may redistribute this software as part of application delivery: however, 
' this copyright notice may not be removed and you may not otherwise redistribute 
' this software directly other than providing a link to wwww.gendotnet.com. 
'
' You may alter this software to better server your needs (I'd like to hear about your 
' changes in order to make the software better). If you alter the softwre, you must  
' clearly mark it as altered and must not be misrepresented as the original software.
' nor wholly your original work.
'*************************************************************************************
Option Strict On
Option Explicit On 

Imports System

Namespace KADGen.CodeGenerationSupport
   Public Class XSLTGeneration
      Inherits MultiPass

#Region "Class level declarations"
      Private mXmlMetadata As Xml.XmlDocument
      Private mXsltTransform As Xml.Xsl.XslCompiledTransform
      Private mExtObjects() As CodeGenerationSupport.ExtObject
      Private mInputFileName As String
      Private mXsltFileName As String
#End Region

#Region "Public properties and methods"
      Public Sub Run( _
                     ByVal node As Xml.XmlNode, _
                     ByVal runUtility As RunUtility, _
                     ByVal statusDirective As IStatusDirective)
         Dim xsltGenDirective As CodeGenerationSupport.IXSLTGenerationDirective
         Dim multiPassDirective As CodeGenerationSupport.IMultiPassDirective

         xsltGenDirective = CType(node, CodeGenerationSupport.IXSLTGenerationDirective)
         multiPassDirective = CType(node, CodeGenerationSupport.IMultiPassDirective)
         Try
            OpenXmlFile(xsltGenDirective, statusDirective)
            OpenXsltFile(xsltGenDirective, statusDirective)
            Me.MultiPass(multiPassDirective, _
                     Nothing, Nothing, _
                     AddressOf XSLTGenerationCallBack, _
                     statusDirective, _
                     runUtility)
         Catch ex As System.Exception
            'Diagnostics.Debug.WriteLine("")
            ' Exception should already be recorded
         End Try
      End Sub
#End Region

#Region "Protected properties and methods"
      Protected Function SetupParams( _
               ByVal filename As String, _
               ByVal xsltParams As CodeGenerationSupport.XSLTParam()) _
               As CodeGenerationSupport.XSLTParam()
         Return KADGen.CodeGenerationSupport.XSLTSupport.GetXSLTParamsFromStylesheet( _
                  filename, "", _
                  xsltParams)
      End Function
#End Region

#Region "Private properties and methods"
      Private Sub OpenXmlFile( _
               ByVal xsltGen As CodeGenerationSupport.IXSLTGenerationDirective, _
               ByVal statusDirective As CodeGenerationSupport.IStatusDirective)
         Dim statusOutput As CodeGenerationSupport.IStatusOutput
         Const source As String = "XsltGeneration.OpenXmlFile"
         If (mXmlMetadata Is Nothing Or mInputFileName Is Nothing) _
                  OrElse mInputFileName <> xsltGen.XSLTFile.InputFileName Then
            Try
               mXmlMetadata = New Xml.XmlDocument
               mXmlMetadata.Load(xsltGen.XSLTFile.InputFileName)
            Catch ex As System.Exception
               statusOutput = statusDirective.AddStatusOutput( _
                        "<none>", _
                        FileTreatment.Unknown)
               statusOutput.AddStatusFailure(source, _
                        "Failure opening input metadata file", ex)
               Throw
            End Try
            Try
               mExtObjects = xsltGen.XSLTFile.ExtObjects
            Catch ex As System.Exception
               statusOutput = statusDirective.AddStatusOutput( _
                        "<none>", _
                        FileTreatment.Unknown)
               statusOutput.AddStatusFailure(source, _
                        "Failure creating extension objects", ex)
               Throw
            End Try
         End If
      End Sub

      Private Sub OpenXsltFile( _
               ByVal xsltGen As CodeGenerationSupport.IXSLTGenerationDirective, _
               ByVal statusDirective As CodeGenerationSupport.IStatusDirective)
         Const source As String = "XsltGeneration.OpenXsltFile"
         Dim statusOutput As CodeGenerationSupport.IStatusOutput
         If (mXsltTransform Is Nothing Or mXsltFileName Is Nothing) _
                 OrElse mXsltFileName <> xsltGen.XSLTFile.XSLTFileName Then
            Try
               mXsltTransform = New Xml.Xsl.XslCompiledTransform(True)
               mXsltTransform.Load(xsltGen.XSLTFile.XSLTFileName)
            Catch ex As System.Exception
               statusOutput = statusDirective.AddStatusOutput( _
                        "<none>", _
                        FileTreatment.Unknown)
               statusOutput.AddStatusFailure(source, _
                        "Failure loading XSLT (Filename=" & _
                              xsltGen.XSLTFile.XSLTFileName, ex)
               Throw
            End Try
            Try
               SetParams(SetupParams( _
                        xsltGen.XSLTFile.XSLTFileName, _
                        xsltGen.XSLTFile.XSLTParams))
            Catch ex As System.Exception
               statusOutput = statusDirective.AddStatusOutput( _
                        "<none>", _
                        FileTreatment.Unknown)
               statusOutput.AddStatusFailure(source, _
                        "Failure creating parameters", ex)
               Throw
            End Try
         End If
      End Sub

      Private Function XSLTGenerationCallBack( _
               ByVal outputFileName As String, _
               ByVal multiPassDirective As CodeGenerationSupport.IMultiPassDirective, _
               ByVal nodeSelect As Xml.XmlNode, _
               ByVal runUtility As CodeGenerationSupport.RunUtility, _
               ByVal statusOutput As CodeGenerationSupport.IStatusOutput) _
               As IO.Stream
         Me.SetParameters(nodeSelect, outputFileName)
         Return CodeGenerationSupport.XSLTSupport.GenerateViaXSLT( _
                              mXsltTransform, mXmlMetadata, _
                              multiPassDirective.OutputRule.OutputFileType.ToString, _
                              mExtObjects, Params)
      End Function

#End Region
   End Class
End Namespace